package cn.stylefeng.guns.modular.biz.timer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.biz.entity.PrWeightSumOrder;
import cn.stylefeng.guns.modular.biz.pojo.request.PrWeightSumOrderRequest;
import cn.stylefeng.guns.modular.biz.pojo.response.PrWeightSumOrderVo;
import cn.stylefeng.guns.modular.biz.service.PrWeightSumOrderService;
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import jakarta.annotation.Resource;
import java.util.List;

@Component
public class GetMesPrWeightSumOrderTimer implements TimerAction {


    @Resource
    private PrWeightSumOrderService prWeightSumOrderService;

    /**
     *
     * @param params 是URL地址："http://localhost:18080/PrWeightSumOrder/getPrWeightSumOrderbyDate";
     */
    @Override
    public void action(String params) {
        RestTemplate restTemplate = new RestTemplate();
        System.out.println("定时任务执行了,地址URL："+params);
        JSONObject requestBody = new JSONObject();

        // 设置请求体中的current_date字段为当前日期
//        requestBody.set("current_date", DateUtil.today());
        requestBody.set("current_date", "2024-08-20");
        // 发送POST请求，获取响应
        ResponseEntity<Object> response = restTemplate.postForEntity(params, requestBody, Object.class);
        // 判断响应状态码是否为2xx成功
        if (response.getStatusCode().is2xxSuccessful()) {
            // 处理成功响应
            System.out.println("请求成功");
            // 获取响应体
            Object responseBody = response.getBody();
            // 将响应体转换为LabMainInfoVo类型的列表
            List<PrWeightSumOrderVo> list= JSONUtil.parseArray(responseBody).toList(PrWeightSumOrderVo.class);
            // 遍历列表
            for (PrWeightSumOrderVo prWeightSumOrderVo : list) {
                // 根据sample_id查询LabMainInfo
                PrWeightSumOrder IsPrWeightSumOrder=prWeightSumOrderService.getOne(new LambdaQueryWrapper<>(PrWeightSumOrder.class).eq(PrWeightSumOrder::getWtSumId,prWeightSumOrderVo.getWtSumId()));
                // 如果查询结果为空
                if(ObjectUtil.isEmpty(IsPrWeightSumOrder)){
                    // 创建LabMainInfoRequest对象
                    PrWeightSumOrderRequest prWeightSumOrderRequest=new PrWeightSumOrderRequest();
                    // 将labMainInfoVo对象中的属性值复制到labMainInfoRequest对象中
                    BeanUtils.copyProperties(prWeightSumOrderVo,prWeightSumOrderRequest);
                    // 添加labMainInfoRequest对象到数据库
                    prWeightSumOrderRequest.setSumDate(DateUtil.formatDate(prWeightSumOrderVo.getSumDate()));
                    prWeightSumOrderService.add(prWeightSumOrderRequest);
                }

            }
        } else {
            // 处理失败响应
        }
    }


}
